package edu.gatech.startrader.gui;

import java.awt.Frame;

import javax.swing.UIManager;

import edu.gatech.startrader.Game;
import edu.gatech.startrader.Game.Difficulty;
import edu.gatech.startrader.player.Player;
import edu.gatech.startrader.player.Skills;
import edu.gatech.startrader.ship.Ship;
import edu.gatech.startrader.ship.ShipType;

import javax.swing.GroupLayout.Alignment;
import javax.swing.GroupLayout;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JLayeredPane;
import javax.swing.ImageIcon;
import javax.swing.JRadioButton;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import net.miginfocom.swing.MigLayout;

/**
 * 
 * @author James
 */
public class PlayerConfigScreen extends javax.swing.JDialog {
	
	public static void configurePlayer(Frame parent)
	{
		PlayerConfigScreen dialog = new PlayerConfigScreen(parent, true);
	    dialog.setVisible(true);
	}
	
	public Player getPlayer()
	{
		return player;
	}
	
	private Game.Difficulty difficulty;
	private Player player;
	
    public PlayerConfigScreen(Frame owner, boolean modal) {
		super(owner, modal);
		getContentPane().setBackground(Color.LIGHT_GRAY);
        initComponents();
    }
                    
    private void initComponents() 
    {
        try {
            for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (Exception e) {
        }
        ButtonGroup buttonGroup1 = new javax.swing.ButtonGroup();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        
        JPanel panel = new JPanel();
        panel.setLayout(null);
        JPanel jPanel2 = new javax.swing.JPanel();
        jPanel2.setBounds(6, 13, 192, 153);
        panel.add(jPanel2);
		
		jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Skills"));
        pilotJSpinner = new javax.swing.JSpinner();
        pilotJSpinner.setBounds(84, 43, 87, 20);
        
        pilotJSpinner.setModel(new javax.swing.SpinnerNumberModel(1, 1, 10, 1));
        pilotJSpinner.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                pilotJSpinnerStateChanged(evt);
            }
        });
        jPanel2.setLayout(null);
        remainingSkillPointsJLabel = new javax.swing.JLabel();
        remainingSkillPointsJLabel.setBounds(21, 23, 150, 16);
        
        remainingSkillPointsJLabel.setText("Remaining Skill Points: 16");
        jPanel2.add(remainingSkillPointsJLabel);
        JLabel pilotJLabel = new javax.swing.JLabel();
        pilotJLabel.setBounds(21, 45, 51, 16);
        
                pilotJLabel.setText("Pilot");
                jPanel2.add(pilotJLabel);
        jPanel2.add(pilotJSpinner);
        JLabel engineerJLabel = new javax.swing.JLabel();
        engineerJLabel.setBounds(21, 69, 51, 16);
        
                engineerJLabel.setText("Engineer");
                jPanel2.add(engineerJLabel);
        engineerJSpinner = new javax.swing.JSpinner();
        engineerJSpinner.setBounds(84, 67, 87, 20);
        
        engineerJSpinner.setModel(new javax.swing.SpinnerNumberModel(1, 1, 10, 1));
        engineerJSpinner.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                engineerJSpinnerStateChanged(evt);
            }
        });
        jPanel2.add(engineerJSpinner);
        JLabel fighterJLabel = new javax.swing.JLabel();
        fighterJLabel.setBounds(21, 93, 51, 16);
        
        fighterJLabel.setText("Fighter");
        jPanel2.add(fighterJLabel);
        traderJSpinner = new javax.swing.JSpinner();
        traderJSpinner.setBounds(84, 115, 87, 20);
        
        traderJSpinner.setModel(new javax.swing.SpinnerNumberModel(1, 1, 10, 1));
        traderJSpinner.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                traderJSpinnerStateChanged(evt);
            }
        });
        fighterJSpinner = new javax.swing.JSpinner();
        fighterJSpinner.setBounds(84, 91, 87, 20);
        
        fighterJSpinner.setModel(new javax.swing.SpinnerNumberModel(1, 1, 10, 1));
        fighterJSpinner.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                fighterJSpinnerStateChanged(evt);
            }
        });
        jPanel2.add(fighterJSpinner);
        JLabel traderJLabel = new javax.swing.JLabel();
        traderJLabel.setBounds(21, 117, 36, 16);
        
        traderJLabel.setText("Trader");
        jPanel2.add(traderJLabel);
        jPanel2.add(traderJSpinner);
        JLabel playerNameJLabel = new javax.swing.JLabel();
        playerNameJLabel.setBounds(210, 24, 88, 14);
        playerNameJLabel.setFont(new Font("Tahoma", Font.PLAIN, 13));
		playerNameJLabel.setForeground(Color.YELLOW);
		panel.add(playerNameJLabel);
	
	    playerNameJLabel.setText("Player Name:");
		playerNameJTextField = new javax.swing.JTextField();
		playerNameJTextField.setBounds(294, 21, 193, 20);
		panel.add(playerNameJTextField);
		JButton doneJButton = new javax.swing.JButton();
		doneJButton.setBounds(284, 130, 78, 23);
		panel.add(doneJButton);
		
        doneJButton.setText("Done");
        doneJButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                doneJButtonMouseClicked(evt);
            }
        });
        JButton cancelJButton = new javax.swing.JButton();
        cancelJButton.setBounds(384, 130, 103, 23);
		panel.add(cancelJButton);
		
        cancelJButton.setText("Cancel");
        cancelJButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
            	setVisible(false);
            	dispose();
            }
        });
		JPanel jPanel1 = new javax.swing.JPanel();
		jPanel1.setBounds(210, 50, 277, 68);
		panel.add(jPanel1);
		JRadioButton hardJRadioButton = new javax.swing.JRadioButton();
		hardJRadioButton.setBounds(222, 16, 49, 23);
		hardJRadioButton.setText("Hard");
		hardJRadioButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
		    	difficulty = Game.Difficulty.HARD;
			}
		});
		JRadioButton impossibleJRadioButton = new javax.swing.JRadioButton();
		impossibleJRadioButton.setBounds(101, 39, 103, 23);
		impossibleJRadioButton.setText("Impossible");
		impossibleJRadioButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
		    	difficulty = Game.Difficulty.IMPOSSIBLE;
			}
		});
		JRadioButton beginnerJRadioButton = new javax.swing.JRadioButton();
		beginnerJRadioButton.setBounds(12, 16, 83, 23);
		beginnerJRadioButton.setText("Beginner");
		beginnerJRadioButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
		    	difficulty = Game.Difficulty.BEGINNER;
			}
		});
		JRadioButton normalJRadioButton = new javax.swing.JRadioButton();
		normalJRadioButton.setBounds(146, 16, 83, 23);
		normalJRadioButton.setText("Normal");
		normalJRadioButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
		    	difficulty = Game.Difficulty.NORMAL;
			}
		});
		JRadioButton easyJRadioButton = new javax.swing.JRadioButton();
		easyJRadioButton.setBounds(91, 16, 49, 23);
		easyJRadioButton.setText("Easy");
		easyJRadioButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
		    	difficulty = Game.Difficulty.EASY;
			}
		});
		
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Game Diffculty"));
		buttonGroup1.add(hardJRadioButton);
		
	    buttonGroup1.add(impossibleJRadioButton);

        buttonGroup1.add(beginnerJRadioButton);
        
        buttonGroup1.add(normalJRadioButton);
        
        buttonGroup1.add(easyJRadioButton);
		jPanel1.setLayout(null);
		jPanel1.add(beginnerJRadioButton);
		jPanel1.add(easyJRadioButton);
		jPanel1.add(normalJRadioButton);
		jPanel1.add(hardJRadioButton);
		jPanel1.add(impossibleJRadioButton);
		
		JLabel lblNewLabel = new JLabel("New label");
		lblNewLabel.setBounds(0, 0, 493, 172);
		lblNewLabel.setIcon(new ImageIcon(PlayerConfigScreen.class.getResource("/edu/gatech/startrader/gui/PlayerConfigScreen.jpg")));
		panel.add(lblNewLabel);
        GroupLayout groupLayout = new GroupLayout(getContentPane());
        groupLayout.setHorizontalGroup(
        	groupLayout.createParallelGroup(Alignment.LEADING)
        		.addComponent(panel, GroupLayout.PREFERRED_SIZE, 493, GroupLayout.PREFERRED_SIZE)
        );
        groupLayout.setVerticalGroup(
        	groupLayout.createParallelGroup(Alignment.LEADING)
        		.addComponent(panel, GroupLayout.PREFERRED_SIZE, 172, GroupLayout.PREFERRED_SIZE)
        );
        getContentPane().setLayout(groupLayout);

        pack();
    }// </editor-fold>                        
    
    private void pilotJSpinnerStateChanged(javax.swing.event.ChangeEvent evt) {                                           
        spinnerStateChanged();
    }                                          

    private void engineerJSpinnerStateChanged(javax.swing.event.ChangeEvent evt) {                                              
        spinnerStateChanged();
    }                                             

    private void fighterJSpinnerStateChanged(javax.swing.event.ChangeEvent evt) {                                             
        spinnerStateChanged();
    }                                            

    private void traderJSpinnerStateChanged(javax.swing.event.ChangeEvent evt) {                                            
        spinnerStateChanged();
    }                                           

    private void doneJButtonMouseClicked(java.awt.event.MouseEvent evt) 
    {                                         
        if(skillTotal() != 20)
        {
            javax.swing.JOptionPane.showMessageDialog(null, "Skill Points Not Legal. Total Must be less than 20");
        }                                         
        else if(playerNameJTextField.getText().equals(""))
        {
            javax.swing.JOptionPane.showMessageDialog(null, "Please enter Player Name");
        }                                         
        else if(difficulty == null)
        {
            javax.swing.JOptionPane.showMessageDialog(null, "Please select game difficulty");
        }
        else
        {
        	int pilot, trader, engineer, fighter;
            pilot = (Integer)(pilotJSpinner.getModel().getValue());
            engineer = (Integer)(engineerJSpinner.getModel().getValue());
            fighter = (Integer)(fighterJSpinner.getModel().getValue());
            trader = (Integer)(traderJSpinner.getModel().getValue());
            Skills playerSkills = new Skills(pilot, fighter, engineer, trader);
            Ship playerShip = new Ship(ShipType.GNAT);
           	player = new Player(playerNameJTextField.getText(), playerSkills, 1000, playerShip);
           	new MainGameScreen(Game.startNewGame(player, difficulty)).setVisible(true);
            this.setVisible(false);
            dispose();
        }
    }                                                    

    private void spinnerStateChanged()
    {
        if(skillTotal() > 20)
        {
            remainingSkillPointsJLabel.setText("Remaining Skill Points: 0");
            repaint();
        }
        else
        {
            remainingSkillPointsJLabel.setText("Remaining Skill Points: " + (20 - skillTotal()));
            repaint();
        }
    }
    
    private int skillTotal()
    {
        int total = 0;
        total += (Integer)(pilotJSpinner.getModel().getValue());
        total += (Integer)(engineerJSpinner.getModel().getValue());
        total += (Integer)(fighterJSpinner.getModel().getValue());
        total += (Integer)(traderJSpinner.getModel().getValue());
        return total;
    }

    private javax.swing.JSpinner engineerJSpinner;
    private javax.swing.JSpinner fighterJSpinner;
    private javax.swing.JSpinner pilotJSpinner;
    private javax.swing.JTextField playerNameJTextField;
    private javax.swing.JLabel remainingSkillPointsJLabel;
    private javax.swing.JSpinner traderJSpinner;
}